document.write('<script type="text/javascript" src="' + context
		+ '/system/wordbook/js/wbstore.js"></script>');
document.write('<script type="text/javascript" src="' + context
		+ '/system/group/js/groupcombo4jsp.js"></script>');
document.write('<link type="text/css" rel="stylesheet" href="' + context
		+ '/system/group/css/groupmanage.css"/>');
document.write('<script type="text/javascript" src="' + context
		+ '/system/role/js/rolecombo4jsp.js"></script>');
document.write('<link type="text/css" rel="stylesheet" href="' + context
		+ '/system/role/css/rolemanage.css"/>');
document.write('<script type="text/javascript" src="' + context
		+ '/system/dataindex/js/dataindexcombox.js"></script>');
document.write('<link type="text/css" rel="stylesheet" href="' + context
		+ '/system/tablequery/css/querylistformcmp.css"/>');
document.write('<script src="' + context
		+ '/system/tablequery/js/querylist4query.js"></script>');
document.write('<script src="' + context
		+ '/system/tablequery/js/querylist4window.js"></script>');
document.write('<script src="' + context
		+ '/system/tablequery/js/querylistformcmp.js"></script>');
document.write('<script src="' + context
		+ '/system/objquery/js/querylist4window.js"></script>');
document.write('<script src="' + context
		+ '/system/object/js/objecthandler.js"></script>');

var dateItemMap = new HashMap();

/**
 * 
 * @param {}
 *            id 模板ID
 * @param {}
 *            pkid 资产对象主键ID
 */
function initPage(id, pkid) {
	var url = context + '/app/gdzctemplate.do?method=loadcomponents';

	Ext.Ajax.request({
		url : url,
		params : {
			tempid : id
		},
		method : 'POST',
		success : function(rs, request) {
			var result = rs.responseText;// 拿到结果集，此时为字符串

			var json = Ext.util.JSON.decode(result);// 将字符串转换为json类型

			if (json.success) {
				/* common item list */
				var commonItems = initComponents(json.data.COMMON);
				/* base item list */
				var baseItems = initComponents(json.data.BASE);
				/* config item list */
				var configItems = initComponents(json.data.CONFIG);
				/* finance item list */
				var financeItems = initComponents(json.data.FINANCE);
				/* mainten item list */
				var maintenItems = initComponents(json.data.MAINTEN);

				var tabPanel = new Ext.TabPanel({
							columnWidth : .6,
							plain : true,
							border : true,
							activeTab : 0,
							height : 500,
							/*
							 * By turning off deferred rendering we are
							 * guaranteeing that the form fields within tabs
							 * that are not activated will still be rendered.
							 * This is often important when creating
							 * multi-tabbed forms.
							 */
							deferredRender : false,
							defaults : {
								bodyStyle : 'padding:10px'
							},
							items : []
						});

				if (baseItems.length > 0) {
					tabPanel.add({
								title : '<font class="oaFont">资产基本信息</font>',
								layout : 'form',
								frame : false,
								border : false,
								defaults : {
									width : 300
								},
								defaultType : 'textfield',

								items : [baseItems]
							});
				}

				if (configItems.length > 0) {
					tabPanel.add({
								title : '<font class="oaFont">配置参数信息</font>',
								layout : 'form',
								frame : false,
								border : false,
								defaults : {
									width : 300
								},
								defaultType : 'textfield',

								items : [configItems]
							});
				}

				if (financeItems.length > 0) {
					tabPanel.add({
								title : '<font class="oaFont">财务接口信息</font>',
								layout : 'form',
								frame : false,
								border : false,
								defaults : {
									width : 300
								},
								defaultType : 'textfield',

								items : [financeItems]
							});
				}

				if (maintenItems.length > 0) {
					tabPanel.add({
								title : '<font class="oaFont">维保管理信息</font>',
								layout : 'form',
								frame : false,
								border : false,
								defaults : {
									width : 300
								},
								defaultType : 'textfield',

								items : [maintenItems]
							});
				}

				var action = 'save';

				if (pkid && pkid != '') {
					action = 'update';
				}

				var form = new Ext.FormPanel({
					id : 'gzform',
					labelAlign : 'left',
					title : '<font class="oaFont">' + json.data.name
							+ '</font>',
					frame : true,
					fileUpload : true,
					height : document.documentElement.clientHeight,
					bodyStyle : 'padding:5px',
					items : [{
						layout : 'column',
						border : false,
						items : [{
							columnWidth : .4,
							xtype : 'fieldset',
							labelWidth : 90,
							title : '<font class="oaFont">资产登记卡片</font>',
							style : {
								'margin-left' : '0px',
								'margin-right' : '10px',
								'padding' : '5px'
							},
							items : {
								layout : 'form',
								defaults : {
									width : 230
								},
								border : false,
								items : commonItems
							},
							buttons : [{
								text : '<font class="oaFont">保存提交</font>',
								handler : function() {
									if (action == 'save') {
										saveObject4Ext('APP_GDZC', form
														.getForm()
														.findField('GZNAME')
														.getValue(), form,
												function() {
													if (form.getForm()
															.isValid()) {
														createHidden('TEMPID',
																id);
														createHidden(
																'TYPEID',
																json.data.typeid);
														createHidden(
																'TYPENAME',
																json.data.typename);
														createHidden(
																'TYPECODE',
																json.data.typecode);

														return true;
													}

													return false;
												}, function() {
													alert('保存成功');
													handleRefresh();
												});
									} else if (action == 'update') {
										updateObject4Ext(pkid, 'APP_GDZC', form
														.getForm()
														.findField('GZNAME')
														.getValue(), form,
												function() {
													if (form.getForm()
															.isValid()) {
														createHidden(
																'oupdatetime',
																nowTime());
														return true;
													}

													return false;
												}, function() {
													alert('保存成功');
													handleRefresh();
												});
									}
								}
							}, {
								text : '<font class="oaFont">重新填写</font>',
								handler : function() {
									form.form.reset();
								}
							}, {
								text : '<font class="oaFont">返回</font>',
								handler : function() {
									window.history.back(-1);
								}
							}]
						}, tabPanel]
					}]
				});

				if (pkid && pkid != '') {
					form.getForm().load({
						url : context + '/app/gdzc.do?method=edit',
						params : {
							id : pkid
						},
						waitTitle : '提示',
						waitMsg : '正在加载数据,请稍候...',
						animEl : 'loding',
						success : function(form, action) {
							var baseId = action.result.data.BASEID;
							createHidden('BASEID', baseId);
							var varcharId = action.result.data.VARCHARID;
							createHidden('VARCHARID', varcharId);
							var integerId = action.result.data.INTEGERID;
							createHidden('INTEGERID', integerId);
							var floatId = action.result.data.FLOATID;
							createHidden('FLOATID', floatId);
							var clobId = action.result.data.CLOBID;
							createHidden('CLOBID', clobId);
							var fileId = action.result.data.FILEID;
							createHidden('FILEID', fileId);
							var objectId = action.result.data.OBJECTID;
							createHidden('OBJECTID', objectId);
							var wbId = action.result.data.WBID;
							createHidden('WBID', wbId);
							var diId = action.result.data.DIID;
							createHidden('DIID', diId);
							var tqId = action.result.data.TQID;
							createHidden('TQID', tqId);

							var dateItemKeys = dateItemMap.keys();

							if (dateItemKeys != null && dateItemKeys.length > 0) {
								for (var i = 0; i < dateItemKeys.length; i++) {
									var dateType = dateItemMap
											.get(dateItemKeys[i]);
									if (dateType == 'date'
											&& action.result.data[dateItemKeys[i]] != '') {
										Ext
												.getCmp('_' + dateItemKeys[i])
												.setValue(formatDate(action.result.data[dateItemKeys[i]]));
									} else if (dateType == 'time'
											&& action.result.data[dateItemKeys[i]] != '') {
										Ext
												.getCmp('_' + dateItemKeys[i])
												.setValue(formatTime(action.result.data[dateItemKeys[i]]));
									}
								}
							}
						},
						failure : function(form, action) {
							if (action.result.errCode
									&& action.result.errCode == '-1') {
								Ext.Msg.alert('读取资产内容失败,未找到对象结构');
							} else if (action.result.errCode
									&& action.result.errCode == '-2') {
								Ext.Msg.alert('读取资产内容失败,没有编辑权限');
							} else if (action.result.errCode
									&& action.result.errCode == '-3') {
								Ext.Msg.alert('读取资产内容失败，读取资产内容失败');
							} else {
								Ext.Msg.alert('未知的异常错误');
							}
						}
					});
				}

				// tab2.render(document.body);

				var viewport = new Ext.Viewport({
							border : false,
							items : [form]
						});
			}
		},
		failure : function(rs, request) {
			Ext.Msg.alert('提示', '未知的异常错误');
		}
	});
}

function initComponents(items) {
	var comps = [];

	Ext.each(items, function(item) {
				var formItem = getExtFormItem(item);
				if (formItem != null) {
					comps.push(formItem);
				}
			});

	return comps;
}

function getExtFormItem(item) {
	var needValue = item.checkblank;

	var vailu = item.dvalue;

	if (item.type == 'text') {

		var it;
		if (item.columntype == 'integer' || item.columntype == 'float') {
			it = new Ext.form.NumberField({
						fieldLabel : item.name,
						id : item.formname,
						name : item.formname,
						allowBlank : !needValue,
						allowDecimals : (item.columntype == 'float'),
						value : vailu
					});
		} else {

			it = new Ext.form.TextField({
						fieldLabel : item.name,
						id : item.formname,
						name : item.formname,
						allowBlank : !needValue,
						value : vailu
					});
		}
		return it;
	} else if (item.type == 'textarea') {
		var it = new Ext.form.TextArea({
					fieldLabel : item.name,
					id : item.formname,
					name : item.formname,
					allowBlank : !needValue,
					value : vailu
				});
		return it;
	} else if (item.type == 'date') {
		dateItemMap.put(item.formname, 'date');

		var it = [new Ext.form.DateField({
					fieldLabel : item.name,
					id : '_' + item.formname,
					name : '_' + item.formname,
					format : 'Y-m-d',
					allowBlank : !needValue,
					value : formatDate(vailu),
					listeners : {
						select : function(m, d) {
							var dateStr = parseDate(Ext.getCmp('_'
									+ item.formname).getEl().dom.value)
									+ '000000';
							Ext.getCmp(item.formname).setValue(dateStr);
						}
					}
				}), {
			xtype : 'hidden',
			name : item.formname,
			id : item.formname,
			value : vailu
		}];
		return it;
	} else if (item.type == 'time') {
		dateItemMap.put(item.formname, 'time');

		var it = [new Ext.form.DateField({
					fieldLabel : item.name,
					id : '_' + item.formname,
					name : '_' + item.formname,
					format : 'Y-m-d H:i:s',
					allowBlank : !needValue,
					value : formatTime(vailu),
					listeners : {
						select : function(m, d) {
							var dateStr = parseDate(Ext.getCmp('_'
									+ item.formname).getEl().dom.value);
							Ext.getCmp(item.formname).setValue(dateStr);
						}
					}
				}), {
			xtype : 'hidden',
			name : item.formname,
			id : item.formname,
			value : vailu
		}];
		return it;
	} else if (item.type == 'select') {
		var it = [
				getWBComboStore(item.param, '', item.formname + '_', item.name,
						item.formname + '_ID', item.formname, '', !needValue),
				{
					xtype : 'hidden',
					name : item.formname,
					id : item.formname,
					value : vailu
				}, {
					xtype : 'hidden',
					name : item.formname + '_ID',
					id : item.formname + '_ID',
					value : vailu
				}];

		return it;
	} else if (item.type == 'guselect') {
		var itemid = item.formname + '_ID';
		var itemname = item.formname;
		var itemtype = item.formname + '_TYPE';
		var it = [
				getGroupUserChkCombo4Single(itemid, itemname, itemtype,
						item.param, !needValue), new Ext.form.Hidden({
							id : itemid,
							name : itemid,
							value : ''
						}), new Ext.form.Hidden({
							id : itemname,
							name : itemname,
							value : ''
						}), new Ext.form.Hidden({
							id : itemtype,
							name : itemtype,
							value : ''
						})]

		Ext.getCmp(itemid + '_').fieldLabel = item.name;

		return it;
	} else if (item.type == 'gselect') {
		var itemid = item.formname + '_ID';
		var itemname = item.formname;
		var itemtype = item.formname + '_TYPE';
		var it = [
				getGroupSelectCombo4Single(itemid, itemname, itemtype,
						item.param, !needValue), new Ext.form.Hidden({
							id : itemid,
							name : itemid,
							value : ''
						}), new Ext.form.Hidden({
							id : itemname,
							name : itemname,
							value : ''
						}), new Ext.form.Hidden({
							id : itemtype,
							name : itemtype,
							value : ''
						})]

		Ext.getCmp(itemid + '_').fieldLabel = item.name;

		return it;
	} else if (item.type == 'uselect') {
		var itemid = item.formname + '_ID';
		var itemname = item.formname;
		var itemtype = item.formname + '_TYPE';
		var it = [
				getUserChkCombo4Single(itemid, itemname, itemtype, item.param,
						!needValue), new Ext.form.Hidden({
							id : itemid,
							name : itemid,
							value : ''
						}), new Ext.form.Hidden({
							id : itemname,
							name : itemname,
							value : ''
						}), new Ext.form.Hidden({
							id : itemtype,
							name : itemtype,
							value : ''
						})]

		Ext.getCmp(itemid + '_').fieldLabel = item.name;

		return it;
	} else if (item.type == 'guselect1') {
		var itemid = item.formname + '_ID';
		var itemname = item.formname;
		var itemtype = item.formname + '_TYPE';
		var it = [
				getGroupUserChkCombo(itemid, itemname, itemtype, item.param,
						!needValue), new Ext.form.Hidden({
							id : itemid,
							name : itemid,
							value : ''
						}), new Ext.form.Hidden({
							id : itemname,
							name : itemname,
							value : ''
						}), new Ext.form.Hidden({
							id : itemtype,
							name : itemtype,
							value : ''
						})]

		Ext.getCmp(itemid + '_').fieldLabel = item.name;

		return it;
	} else if (item.type == 'gselect1') {
		var itemid = item.formname + '_ID';
		var itemname = item.formname;
		var itemtype = item.formname + '_TYPE';
		var it = [
				getGroupSelectCombo(itemid, itemname, itemtype, item.param,
						!needValue), new Ext.form.Hidden({
							id : itemid,
							name : itemid,
							value : ''
						}), new Ext.form.Hidden({
							id : itemname,
							name : itemname,
							value : ''
						}), new Ext.form.Hidden({
							id : itemtype,
							name : itemtype,
							value : ''
						})]

		Ext.getCmp(itemid + '_').fieldLabel = item.name;

		return it;
	} else if (item.type == 'uselect1') {
		var itemid = item.formname + '_ID';
		var itemname = item.formname;
		var itemtype = item.formname + '_TYPE';
		var it = [
				getUserChkCombo(itemid, itemname, itemtype, item.param,
						!needValue), new Ext.form.Hidden({
							id : itemid,
							name : itemid,
							value : ''
						}), new Ext.form.Hidden({
							id : itemname,
							name : itemname,
							value : ''
						}), new Ext.form.Hidden({
							id : itemtype,
							name : itemtype,
							value : ''
						})]

		Ext.getCmp(itemid + '_').fieldLabel = item.name;

		return it;
	} else if (item.type == 'roleselect') {
		var itemid = item.formname + '_ID';
		var itemname = item.formname;
		var itemtype = item.formname + '_TYPE';
		var it = [
				getRoleSingleSelectCombo(itemid, itemname, itemtype, '',
						!needValue), new Ext.form.Hidden({
							id : itemid,
							name : itemid,
							value : ''
						}), new Ext.form.Hidden({
							id : itemname,
							name : itemname,
							value : ''
						}), new Ext.form.Hidden({
							id : itemtype,
							name : itemtype,
							value : ''
						})]
		Ext.getCmp(itemid + '_').fieldLabel = item.name;

		return it;
	} else if (item.type == 'roleselect1') {
		var itemid = item.formname + '_ID';
		var itemname = item.formname;
		var itemtype = item.formname + '_TYPE';
		var it = [
				getRoleSelectCombo(itemid, itemname, itemtype, '', !needValue),
				new Ext.form.Hidden({
							id : itemid,
							name : itemid,
							value : ''
						}), new Ext.form.Hidden({
							id : itemid + '_name',
							name : itemid + '_name',
							value : ''
						}), new Ext.form.Hidden({
							id : itemid + '_type',
							name : itemid + '_type',
							value : ''
						})]
		Ext.getCmp(itemid + '_').fieldLabel = item.name;

		return it;
	} else if (item.type == 'dataindex') {
		var itemid = item.formname + '_ID';
		var itemname = item.formname;
		var itemtype = item.formname + '_TYPE';

		var it = [
				getDataindexCombo(itemid, itemname, itemtype, item.param,
						!needValue), new Ext.form.Hidden({
							id : itemid,
							name : itemid,
							value : ''
						}), new Ext.form.Hidden({
							id : itemname,
							name : itemname,
							value : ''
						}), new Ext.form.Hidden({
							id : itemtype,
							name : itemtype,
							value : ''
						})]
		Ext.getCmp(itemid + '_').fieldLabel = item.name;

		return it;
	} else if (item.type == 'tablequery') {
		var itemid = item.formname + '_ID';
		var itemname = item.formname;
		var itemtype = item.formname + '_TYPE';
		var qk;
		var pointId;
		var pointName;

		if (item.param != '') {
			try {
				var json = Ext.util.JSON.decode(item.param);
				qk = json.queryKey;
				pointId = json.pointId;
				pointName = json.pointName;
			} catch (error) {
				alert('模板项<' + item.label + '>的特殊参数-- ' + item.param
						+ ' --定义有误,需要是一个可识别为json的参数');
			}
		}

		var it = [new Ext.form.TriggerField({
					id : itemname,
					name : itemname,
					allowBlank : !needValue,
					triggerClass : 'my-trigger-class',
					fieldLabel : item.name,
					value : '',
					readOnly : true,
					hideTrigger : true,
					enableKeyEvents : true,
					listeners : {
						'focus' : function() {
							getTableQueryWindow(qk, null, function(record) {
										if (record != null) {
											$(itemname).value = record
													.get(pointName);
										} else {
											$(itemname).value = '';
										}
									}, itemid, pointId, pointName, !needValue)
						}
					},
					onTriggerClick : function(e) {
					}

				}), new Ext.form.Hidden({
					id : itemid,
					name : itemid,
					value : ''
				}), new Ext.form.Hidden({
					id : itemtype,
					name : itemtype,
					value : ''
				})]

		return it;
	} else if (item.type == 'objectquery') {
		var itemid = item.formname + '_ID';
		var itemname = item.formname;
		var itemtype = item.formname + '_TYPE';

		var qk;
		var pointId;
		var pointName;

		if (item.param != '') {
			try {
				var json = Ext.util.JSON.decode(item.param);
				qk = json.queryKey;
				pointId = json.pointId;
				pointName = json.pointName;
			} catch (error) {
				alert('模板项<' + item.label + '>的特殊参数-- ' + item.param
						+ ' --定义有误,需要是一个可识别为json的参数');
			}
		}

		var it = [new Ext.form.TriggerField({
					id : itemname,
					name : itemname,
					allowBlank : !needValue,
					triggerClass : 'my-trigger-class',
					fieldLabel : item.name,
					value : '',
					readOnly : true,
					hideTrigger : true,
					enableKeyEvents : true,
					listeners : {
						'focus' : function() {
							getOQueryWindow(qk, null, function(record) {
										if (record != null) {
											$(itemname).value = record
													.get(pointName);
										} else {
											$(itemname).value = '';
										}
									}, itemid, pointId, pointName, !needValue)
						}
					},
					onTriggerClick : function(e) {
						getOQueryWindow(qk, null, function(record) {
									if (record != null) {
										$(itemname).value = record
												.get(pointName);
									} else {
										$(itemname).value = '';
									}
								}, itemid, pointId, pointName, !needValue)
					}

				}), new Ext.form.Hidden({
					id : itemid,
					name : itemid,
					value : ''
				}), new Ext.form.Hidden({
					id : itemtype,
					name : itemtype,
					value : ''
				})];
		return it;
	} else if (item.type == 'file') {
		var it = new Ext.form.TextField({
					fieldLabel : item.name,
					id : item.formname,
					name : item.formname,
					inputType : 'file',
					allowBlank : !needValue,
					value : ''
				});
		return it;
	} else if (item.type == 'checkbox') {
		var it = new Ext.form.CheckboxGroup({
					fieldLabel : item.name,
					items : []
				});
		it.items.push({
					boxLabel : '&nbsp;',
					name : item.formname,
					inputValue : 1
				});
		return it;
	} else if (item.type == 'radio') {
		var it = new Ext.form.RadioGroup({
					fieldLabel : item.name,
					items : []
				});
		if (item.param != '') {
			try {
				var json = Ext.util.JSON.decode(item.param);

				Ext.each(json, function(pm) {
							if (pm != null && pm.length == 3) {
								it.items.push({
											boxLabel : pm[0],
											name : item.formname,
											inputValue : pm[1],
											checked : (pm[2] == 'checked')
										});
							} else if (pm != null && pm.length == 2) {
								it.items.push({
											boxLabel : pm[0],
											name : item.formname,
											inputValue : (pm[1] == 'checked')
										});
							}
						});
			} catch (error) {
				alert('模板项<' + item.label + '>的特殊参数-- ' + item.param
						+ ' --定义有误,需要是一个可识别为json的参数');
			}

		} else {
			it.items.push({
						boxLabel : '是',
						name : item.formname,
						inputValue : 1
					});
			it.items.push({
						boxLabel : '否',
						name : item.formname,
						inputValue : 0
					});
		}
		return it;
	}
}